package com.energyfuture.system.config;

import java.util.Date;
import java.util.List;

import com.energyfuture.system.api.domain.SysUser;
import com.energyfuture.system.api.model.SysparaEntity;
import com.energyfuture.system.service.ISysParamService;
import com.energyfuture.system.service.ISysUserService;

public class ScheduledTask implements Runnable {



    private ISysUserService userService ;

    private ISysParamService sysParamService;

    private String id ;

    public ScheduledTask(String id,ISysUserService userService, ISysParamService sysParamService) {
        this.userService = userService;
        this.sysParamService = sysParamService;
        this.id = id;
    }

    @Override
    public void run() {
        Date now = new Date();
        List<SysUser> sysUsers = userService.selectList(new SysUser());
        SysparaEntity sysparaEntity = sysParamService.selectParam();
        Long lszhsysj = Long.valueOf(sysparaEntity.getLszhsysj() == null ? "90" : sysparaEntity.getLszhsysj()) * 24 * 60 * 60 * 1000;
        Long lszhxmsj = Long.valueOf(sysparaEntity.getLszhxmsj() == null ? "365" : sysparaEntity.getLszhxmsj()) * 24 * 60 * 60 * 1000;
        Long cqzhxmsj = Long.valueOf(sysparaEntity.getCqzhxmsj() == null ? "90" : sysparaEntity.getCqzhxmsj()) * 24 * 60 * 60 * 1000;
        Long cqzhsdsj = Long.valueOf(sysparaEntity.getCqzhsdsj() == null ? "365" : sysparaEntity.getCqzhsdsj()) * 24 * 60 * 60 * 1000;
        for (SysUser sysUser : sysUsers) {
            if (sysUser.getAccountStatus() != null && sysUser.getAccountStatus() == 1 && sysUser.getAccountFlag() != null && sysUser.getAccountFlag() == 2 && sysUser.getAccountStatusTime() != null && (now.getTime() - sysUser.getAccountStatusTime().getTime()) > lszhsysj) {
                sysUser.setAccountFlag(0);
                sysUser.setAccountStatusTime(now);
                userService.updateUser(sysUser);
            } else if (sysUser.getAccountStatus() != null && sysUser.getAccountStatus() == 1 && sysUser.getAccountFlag() != null && sysUser.getAccountFlag() == 0 && sysUser.getAccountStatusTime() != null && (now.getTime() - sysUser.getAccountStatusTime().getTime()) > lszhxmsj) {
                sysUser.setAccountFlag(1);
                sysUser.setAccountStatusTime(now);
                userService.updateUser(sysUser);
            } else if (sysUser.getAccountStatus() != null && sysUser.getAccountStatus() == 0 && sysUser.getAccountFlag() != null && sysUser.getAccountFlag() == 2 && sysUser.getAccountStatusTime() != null && (now.getTime() - sysUser.getAccountStatusTime().getTime()) > cqzhsdsj) {
                sysUser.setAccountFlag(0);
                sysUser.setAccountStatusTime(now);
                userService.updateUser(sysUser);
            } else if (sysUser.getAccountStatus() != null && sysUser.getAccountStatus() == 0 && sysUser.getAccountFlag() != null && sysUser.getAccountFlag() == 0 && sysUser.getAccountStatusTime() != null && (now.getTime() - sysUser.getAccountStatusTime().getTime()) > cqzhxmsj) {
                sysUser.setAccountFlag(1);
                sysUser.setAccountStatusTime(now);
                userService.updateUser(sysUser);
            }
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
